package com.jackson.springaop.aspect;

/*
 * Author: JacksonCoder
 * FileName: LoggAspect
 * Version: 1.0.0
 * Date: 2025/6/25
 */

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggAspect {

    @Around("@annotation(logExecution)")  // 拦截带有 @LogExecution 注解的方法
    public Object logMethodExecution(ProceedingJoinPoint joinPoint, LogExecution logExecution) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        String prefix = logExecution.value().isEmpty() ? "" : logExecution.value() + " - ";

        System.out.println(prefix + "【开始】执行方法: " + methodName);

        try {
            Object result = joinPoint.proceed();

            System.out.println(prefix + "【结束】执行方法: " + methodName);
            return result;
        } catch (Exception e) {
            System.out.println(prefix + "【异常】执行方法: " + methodName + ", 错误:" + e.getMessage());
            throw e;
        }

    }

}
